package com.team.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class GlobalFilterDemo implements GlobalFilter,Ordered {
    //过滤器实际逻辑
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest().mutate().header("demo", "global").build();
        log.info("前置逻辑");
        return chain.filter(exchange.mutate().request(request).build()).then(Mono.fromRunnable(()->{
            log.info("后置逻辑");
                }
        ));
    }

    //优先级
    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE;
    }
}
